$ontext
Uncapaciated Facility Location problem with controlled M

See:
http://coral.ie.lehigh.edu/~coin/COIN_EXAMPLES/uflOSI/Doc/uflosi/node1.html

Sets
	i potential facilty locations /i1*i90/
	j clients                  /j1*j90/;
$offtext

$batinclude "setparam.inc"

Parameters
	b(j) demand from clients j 
	r(i) cost to open at location i 
	c(i,j) per-unit cost to ship from i to j 
	xf(i) x coordinate of facility location
	yf(i) y coordinate of facility location  
	xc(j) x coordinate of client
	yc(j) y coordinate of client;
*Open cost if fixed at 1
*r(i) = 1;
r(i) = uniform(0.8, 1.2);
	
Options SEED = 1222;
*Generate random demand from clients
b(j) = uniform(0,1);

*Generate facilities and clients on the unit square
xf(i) = uniform(0,1);
yf(i) = 1 - uniform(0,1);
xc(j) = 1 - uniform(0,1);
yc(j) = uniform(0,1);

*c(i,j) is the euclidean distance of the facility and 
c(i,j) = sqrt(power(xf(i)-xc(j),2) + power(yf(i)-yc(j),2));

Scalars
	p penalty for unmet demand at any customer 
	M maximum amount of product to be shipped out of any location "big M"
	rbar total budget for opening locations;
p = 1000*smax((i,j),c(i,j));
*M = sum(j,b(j));
M = 40;
rbar = floor(0.5*card(i));
*rbar = 2;

Parameters
	MW(i) M for weak formulation
	MS(i,j) M for strong formulation
        MWC Cheated M for weak formulation;
MS(i,j) = 1;
Variables
	x(i)   open at location j or not
	y(i,j) number of shipped units from i to j
	yp(j)  unmet demand at j
	z      total cost;
Binary variable x;
Positive variable y, yp;

Equations
	cost
	c1(i)     shippment bounded by conservative M
	c1w(i)    shippment bounded by cheating M for weak formulation
	c1s(i,j)  shippment bounded by cheating M for strong formulation
	c2(j)
	c3;
cost..      z =e= sum((i,j),c(i,j)*y(i,j))+sum(j,p*yp(j));
c1(i)..     sum(j,y(i,j)) =l= M*x(i);
c1w(i)..    sum(j,y(i,j)) =l= MW(i)*x(i);
c1s(i,j)..  y(i,j) =l= MS(i,j)*x(i);
*c1s(i,j)..  y(i,j)/b(j) =l= MS(i,j)*x(i);
c2(j)..     sum(i,y(i,j)) + yp(j) =g= b(j);
c3..        sum(i,r(i)*x(i)) =l= rbar;

option optcr=0.005, mip=convert;

* Using conservative M
Model uflp /cost,c1s,c2,c3/;
uflp.optfile = 1;

Solve uflp using mip minimizing z;
MWC = smax((i,j),y.L(i,j));
display MWC;


$Ontext

Using conservative M
Model uflp /cost,c1,c2,c3/;
Solve uflp using mip minimizing z;

 Make cheated Ms
MW(i) = sum(j,y.L(i,j)*x.L(i));
MS(i) = smax(j,y.L(i,j)*x.L(i));

Weak model with cheating M
Model wuflp /cost,c1w,c2,c3/;
Solve wuflp using mip minimizing z;

Strong model with cheating M
Model suflp /cost,c1s,c2,c3/;
Solve suflp using mip minimizing z;
Display M, MW, MS;

$OffText

